// pages/archives/archives.js
const app = getApp();
var util = require("../../utils/utils.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var activityId = options.activityId;
    var childId = options.childId;
    this.queryLatelyActivity(activityId, childId);
  },
  queryLatelyActivity: function (activityId, childId) {
    var that = this;
    var params = '';
    if (activityId && childId) {
      params = '?activityId=' + activityId + "&childId=" + childId;
    }
    // 小孩最近参加的活动
    wx.request({
      url: app.httpUrl + '/activity/queryLatelyActivity' + params,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        if (res.data.code == '000') {
          if (res.data.data == null) {
            that.setData({
              hadData: false
            })
            return;
          }
          var activity = res.data.data;
          activity.startDateStr = util.formatDate(activity.startDate, 'M月D日');
          activity.endDateStr = util.formatDate(activity.endDate, 'M月D日');
          that.setData({
            hadData: true,
            activity: activity
          })
          that.queryChildInfo();
          this.queryChildComment();
        }
      }
    })
  },
  queryChildInfo: function () {
    let that = this
    // 首页头部地区接口
    wx.request({
      url: app.httpUrl + '/activity/queryChildInfo',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        activityChildId: this.data.activity.activityChildId
      },
      success: res => {
        if (res.data.code == '000') {
          var childInfo = res.data.data;
          childInfo.childSexStr = childInfo.childSex == 'F' ? '男孩' : '女孩';
          that.setData({
            childInfo: childInfo
          })
        }
      }
    })
  },
  queryChildComment: function () {
    let that = this
    // 首页头部地区接口
    wx.request({
      url: app.httpUrl + '/activity/tutor/queryChildComment',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        activityChildId: this.data.activity.activityChildId
      },
      success: res => {
        if (res.data.code == '000') {
          var list = res.data.data;
          var currTime = new Date().getTime();
          for(var i in list) {
            list[i].keyList = list[i].keywords.split(",");
            var commentDate = list[i].commentDate;
            var commentTime = commentDate.replace(/(.{4})(.*)/, "$1-$2").replace(/(.{7})(.*)/, "$1-$2");
            var totalDay = parseInt(Math.ceil((new Date(commentTime) - that.data.activity.startDate) / 1000 / 60 / 60 / 24)) + 1;
            list[i].day = "Day" + totalDay;
          }
          that.setData({
            commentList: list
          })
        }
      }
    })
  },
  showAllComment: function() {
    this.setData({
      isShowAll: true 
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})